<?php

/**
 * This is the model class for table "ykl_xdvert".
 *
 * The followings are the available columns in table 'ykl_xdvert':
 * @property integer $id
 * @property string $picture
 * @property string $url
 * @property integer $companyid
 * @property integer $hit
 * @property integer $usable
 */
class Xdvert extends CActiveRecord
{

	var $company_title='';
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Xdvert the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'ykl_xdvert';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('companyid,title', 'required','message'=>'不能为空'),
			array('companyid, hit, usable', 'numerical', 'integerOnly'=>true),
			array('url,title', 'length', 'max'=>126),
			//验证企业picture的上传条件 
		 	array('picture',
	            'file', //定义为file类型
	            'allowEmpty' => true,
	            'types' => 'jpg,png,gif', //上传文件的类型
	            'maxSize' => 1024 * 1024 * 3, //上传大小限制，注意不是php.ini中的上传文件大小
	            'tooLarge' => '文件大于3M，上传失败！请上传小于3M的文件！'
            ),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, picture, title,url, companyid, hit, usable,company_title,xdtype,synchronous,thumbnails', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'company'=>array(self::BELONGS_TO,'Company','companyid'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'picture' => '图片',
			'title' => '名称', 
			'url' => '链接地址',
			'companyid' => '企业id',
			'hit' => '点击量',
			'usable' => '是否启用',
			'company_title' =>'企业名称',
			'xdtype' =>'类型',
			'synchronous' =>'是否同步',
			'thumbnails' =>'缩略图',
		);
	}


	/**
	 * 广告类型
	 **/
	public function getXdvertType($id)
	{
		 $xdverttype=array(
                array("id"=>0,"title"=>"图片轮播图"),
                array("id"=>1,"title"=>"视频广告"), 
                array("id"=>2,"title"=>"门户轮播图"), 
                array("id"=>3,"title"=>"推荐视频"), 
                );
        if($id==""){
             return  $xdverttype;
        }else{
            foreach ($xdverttype as $key => $value) {
               if($value['id']==$id){
                return $value['title']; 
               }
            }
        }
	}
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('picture',$this->picture,true);
		$criteria->compare('url',$this->url,true);
		$criteria->compare('companyid',$this->companyid);
		$criteria->compare('hit',$this->hit);
		$criteria->compare('usable',$this->usable);
		$criteria->compare('usable',$this->usable);
		$criteria->compare('xdtype',$this->xdtype);
		$criteria->compare('company.title',$this->company_title,true);
		$criteria->order="id desc";

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}